package com.game.doudizhu.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.game.doudizhu.common.constants.GameConstants;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
import java.util.ArrayList;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.time.LocalDateTime;

@Data
@Entity
@Table(name = "players")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "player_type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("NORMAL")  // 普通玩家的鉴别值
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "password"})
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable = false, unique = true)
    private String username;
    
    @JsonIgnore
    @Column(nullable = false)
    private String password;
    
    @Column(nullable = false)
    private Integer score = 0;
    
    @Column(nullable = false)
    private Integer gamesPlayed = 0;
    
    @Column(nullable = false)
    private Integer gamesWon = 0;
    
    @Column(nullable = false)
    private Integer landlordGames = 0;
    
    @Column(nullable = false)
    private Integer landlordWins = 0;
    
    @Column(nullable = false)
    private Integer farmerGames = 0;
    
    @Column(nullable = false)
    private Integer farmerWins = 0;
    
    @Transient // 不持久化到数据库
    private boolean ready;
    
    @Transient
    private String role;
    
    @Transient
    private List<Card> cards;
    
    // 在 Player 类中添加状态字段
    private String status = "ACTIVE"; // 默认状态为激活
    
    // 在 Player 类中添加 registerTime 字段
    @Column(name = "register_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime registerTime;
    
    @Column(name = "highest_score", nullable = false)
    private Integer highestScore = 0;
    
    @Column(nullable = false)
    private Boolean online = false;  // 添加在线状态字段并设置默认值
    
    @Transient
    private double winRate;
    
    @Transient
    private double averageScore;
    
    // 构造函数
    public Player() {
        this.score = 0;
        this.highestScore = 0;
        this.gamesPlayed = 0;
        this.gamesWon = 0;
        this.landlordGames = 0;
        this.landlordWins = 0;
        this.farmerGames = 0;
        this.farmerWins = 0;
        this.ready = false;
        this.cards = new ArrayList<>();
        this.registerTime = LocalDateTime.now();
        this.status = "ACTIVE"; // 确保设置默认状态
        this.online = false;  // 初始化在线状态
    }
    
    // 更新游戏统计信息
    public void updateGameStats(boolean won, int gameScore, String gameRole) {
        this.gamesPlayed++;
        if (won) {
            this.gamesWon++;
        }
        this.score += gameScore;
        
        if (this.score > this.highestScore) {
            this.highestScore = this.score;
        }
        
        if (GameConstants.Role.LANDLORD.equals(gameRole)) {
            this.landlordGames++;
            if (won) {
                this.landlordWins++;
            }
        } else if (GameConstants.Role.FARMER.equals(gameRole)) {
            this.farmerGames++;
            if (won) {
                this.farmerWins++;
            }
        }
    }
    
    // 重置游戏状态
    public void resetGameState() {
        this.ready = false;
        this.role = GameConstants.Role.UNKNOWN;
        this.score = GameConstants.Game.INIT_SCORE;
        this.cards.clear();
    }
    
    // 计算胜率
    public double getWinRate() {
        return gamesPlayed == 0 ? 0 : (double) gamesWon / gamesPlayed;
    }
    
    // 计算平均分
    public double getAverageScore() {
        return gamesPlayed == 0 ? 0 : (double) score / gamesPlayed;
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Player player = (Player) o;
        return id != null && id.equals(player.id);
    }
    
    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    // 添加 getter 和 setter
    public LocalDateTime getRegisterTime() {
        return registerTime;
    }
    
    public void setRegisterTime(LocalDateTime registerTime) {
        this.registerTime = registerTime;
    }
    
    public Integer getHighestScore() {
        return highestScore;
    }
    
    public void setHighestScore(Integer highestScore) {
        this.highestScore = highestScore;
    }
    
    public Boolean getOnline() {
        return online;
    }
    
    public void setOnline(Boolean online) {
        this.online = online;
    }
    
    public boolean isAI() {
        return "AI".equals(this.getClass().getAnnotation(DiscriminatorValue.class).value());
    }
} 